package com.anolesoft.amljmes.test.fenguangyingdu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.amljmes.test.fenguangyingdu.dto.*;
import com.anolesoft.amljmes.test.fenguangyingdu.entity.*;
import com.anolesoft.amljmes.test.fenguangyingdu.mapper.*;
import com.anolesoft.amljmes.test.fenguangyingdu.service.FenguangYingduService;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2021/12/22 11:38
 */
@Service
@Slf4j
public class FenguanYingduServiceImpl implements FenguangYingduService {
    @Autowired
    private FenguangYingduRecordQueryMapper fenguangYingduRecordQueryMapper;
    @Autowired
    private FenguangYingduRecordLogQueryMapper fenguangYingduRecordLogQueryMapper;
    @Autowired
    private FenguangYingduRecordMapper fenguangYingduRecordMapper;
    @Autowired
    private FenguangYingduRecordLogMapper fenguangYingduRecordLogMapper;
    @Autowired
    private FabrecordMapper fabrecordMapper;
    @Autowired
    private FenguangYingduEmpMapper fenguangYingduEmpMapper;

    @Override
    public PageData<List<FenguangYingduRecordQueryDTO>> selectListByCondition(FenguangYingduRecordQueryDTO fenguangYingduRecordQueryDTO, Integer limit, Integer curr) {
        Page<FenguangYingduRecordQueryDO> page = new Page<>(curr, limit);
        List<FenguangYingduRecordQueryDO> fenguangYingduRecordQueryDOS = fenguangYingduRecordQueryMapper.selectPageList(FenguangYingduRecordQueryDO.builder()
                .jixing(CommonUtils.stringNull(fenguangYingduRecordQueryDTO.getJixing()))
                .tuhao(CommonUtils.warpStringNull(fenguangYingduRecordQueryDTO.getTuhao()))
                .createUserName(CommonUtils.stringNull(fenguangYingduRecordQueryDTO.getCreateUserName()))
                .state(fenguangYingduRecordQueryDTO.getState())
                .build(), page);
        List<FenguangYingduRecordQueryDTO> crmBasePartPriceQueryDTOS = fenguangYingduRecordQueryDOS.stream()
                .map(t -> BeanUtil.copyProperties(t, FenguangYingduRecordQueryDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceQueryDTOS);
    }

    @Override
    public List<FenguangYingduRecordQueryDTO> selectListByCondition(FenguangYingduRecordQueryDTO fenguangYingduRecordQueryDTO) {
        List<FenguangYingduRecordQueryDO> fenguangYingduRecordQueryDOS = fenguangYingduRecordQueryMapper.selectList(FenguangYingduRecordQueryDO.builder()
                .jixing(CommonUtils.stringNull(fenguangYingduRecordQueryDTO.getJixing()))
                .tuhao(CommonUtils.warpStringNull(fenguangYingduRecordQueryDTO.getTuhao()))
                .createUserName(CommonUtils.stringNull(fenguangYingduRecordQueryDTO.getCreateUserName()))
                .state(fenguangYingduRecordQueryDTO.getState())
                .build());
        List<FenguangYingduRecordQueryDTO> dtos = fenguangYingduRecordQueryDOS.stream()
                .map(t -> BeanUtil.copyProperties(t, FenguangYingduRecordQueryDTO.class))
                .collect(Collectors.toList());
        return dtos;
    }

    @Override
    public PageData<List<FenguangYingduRecordLogQueryDTO>> selectListByCondition(FenguangYingduRecordLogQueryDTO fenguangYingduRecordLogQueryDTO, Integer limit, Integer curr) {
        Page<FenguangYingduRecordLogQueryDO> page = new Page<>(curr, limit);
        List<FenguangYingduRecordLogQueryDO> dos = fenguangYingduRecordLogQueryMapper.selectPageList(FenguangYingduRecordLogQueryDO.builder()
                .jixing(CommonUtils.stringNull(fenguangYingduRecordLogQueryDTO.getJixing()))
                .tuhao(CommonUtils.warpStringNull(fenguangYingduRecordLogQueryDTO.getTuhao()))
                .createUserName(CommonUtils.stringNull(fenguangYingduRecordLogQueryDTO.getCreateUserName()))
                .state(fenguangYingduRecordLogQueryDTO.getState())
                .xiugairenName(CommonUtils.stringNull(fenguangYingduRecordLogQueryDTO.getXiugairenName()))
                .build(), page);
        List<FenguangYingduRecordLogQueryDTO> dtos = dos.stream()
                .map(t -> BeanUtil.copyProperties(t, FenguangYingduRecordLogQueryDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), dtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int create(FenguangYingduRecordDTO fenguangYingduRecordDTO) {
        fenguangYingduRecordDTO.setState(0);
        return fenguangYingduRecordMapper.insert(BeanUtil.copyProperties(fenguangYingduRecordDTO, FenguangYingduRecordDO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBatch(List<FenguangYingduRecordDTO> fenguangYingduRecords) {
        fenguangYingduRecords.forEach(t ->
                {
                    FenguangYingduRecordDO source = BeanUtil.copyProperties(t, FenguangYingduRecordDO.class);
                    source.setObjuid(CommonUtils.fastUUID());
                    source.setJianbiaotiaoma("JB39-");
                    source.setState(0);
                    fenguangYingduRecordMapper.insert(source);
                }
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(String id) {
        return fenguangYingduRecordMapper.delete(id);
    }

    @Override
    public FabrecordDTO queryFabrecordByCode(String code) {
        FabrecordDO source = this.fabrecordMapper.selectFabrecordByCode(code);
        if (Objects.isNull(source)) {
            return null;
        }
        return BeanUtil.copyProperties(source, FabrecordDTO.class);
    }

    @Override
    public FenguangYingduEmpDTO queryCheckByCode(String code) {
        FenguangYingduEmpDO source = this.fenguangYingduEmpMapper.selectEmpBypassword2(code);
        if (Objects.isNull(source)) {
            return null;
        }
        return BeanUtil.copyProperties(source, FenguangYingduEmpDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyState(List<String> ids, Integer state) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String updateUser = loginUser.getEmpUid();
        if(1 == state.intValue()){
            ids.forEach(t->this.fenguangYingduRecordMapper.updateStateForApply(state,updateUser,t));
        }else if(2 == state.intValue()){
            ids.forEach(t->this.fenguangYingduRecordMapper.updateStateForAgree(state,updateUser,t));
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(FenguangYingduRecordDTO dto){
        FenguangYingduRecordDO build = FenguangYingduRecordDO.builder()
                .fenguangshuliang(dto.getFenguangshuliang())
                .yingdushuliang(dto.getYingdushuliang())
                .yingdushicezhi(dto.getYingdushicezhi())
                .beizhu(dto.getBeizhu())
                .state(0)
                .objuid(dto.getObjuid())
                .build();
        //查询原数据
        FenguangYingduRecordDO recordDO = this.fenguangYingduRecordMapper.selectOneById(dto.getObjuid());

        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String updateUser = loginUser.getEmpUid();
        FenguangYingduRecordLogDO logDO = BeanUtil.copyProperties(recordDO, FenguangYingduRecordLogDO.class);
        logDO.setObjuid(CommonUtils.fastUUID());
        logDO.setRecordId(recordDO.getObjuid());
        logDO.setFenguangshuliangNew(dto.getFenguangshuliang());
        logDO.setYingdushuliangNew(dto.getYingdushuliang());
        logDO.setYingdushicezhiNew(dto.getYingdushicezhi());
        logDO.setBeizhuNew(dto.getBeizhu());
        logDO.setXiuggairen(updateUser);
        logDO.setXiuggaishijian(LocalDateTime.now());
        this.fenguangYingduRecordLogMapper.insert(logDO);
        log.debug("{}",build);
        this.fenguangYingduRecordMapper.update(build);
    }
}
